﻿
$.MGJ.JackpotManage = [];

(function () {
    var _this = $.MGJ.JackpotManage;
    var psize = 3;
    var clickNum = 1;
    var Num = 1;
    $.extend($.MGJ.JackpotManage, {
        // 创建优惠券
        Dialog_CreateCouponObj: null,
        Dialog_CreateCoupon: function () {
            this.Dialog_CreateCouponObj = $.L.dialog({
                url: ACTION_URL("CreateCoupon", "Market"),
                title: "创建优惠券",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#YHJform").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddYHQInfo();//提交数据

                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        // 创建红包
        Dialog_CreateRPObj: null,
        Dialog_CreateRP: function () {
            this.Dialog_CreateRPObj = $.L.dialog({
                url: ACTION_URL("CreateRP", "Market"),
                title: "创建红包",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var text = $('.myTabs li.active > a.RedPacket').text();
                            if (text == '拼手气红包') {
                                var bootstrapValidator = $("#LuckyRP").data('bootstrapValidator').validate();//触发全部验证
                            }
                            else {
                                var bootstrapValidator = $("#NormalRP").data('bootstrapValidator').validate();//触发全部验证
                            }
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddRPInfo(text);//提交数据

                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        // 创建流量包
        Dialog_CreateFlowObj: null,
        Dialog_CreateFlow: function () {
            this.Dialog_CreateFlowObj = $.L.dialog({
                url: ACTION_URL("CreateFlow", "Market"),
                title: "创建流量包",
                width: 850,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#LLBPacket").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddLLBInfo();//提交数据

                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        }
    });

})(jQuery);
(function () {
    var _this = $.MGJ.JackpotManage;
    $.extend($.MGJ.JackpotManage, {
        //提交优惠券
        Ajax_AddYHQInfo: function () {
            var data = $("#YHJform").serialize();
            $.L.ajaxHandle({
                url: ACTION_URL("JackpotAdd", "Market"),
                data: data,
                success: function (response) {
                    if (response == 1) {
                        $.MGJ.JackpotManage.JackpotTable(psize, 2);//渲染列表
                        _this.Dialog_CreateCouponObj.close();
                        $.L.msgSuccess("创建优惠券成功！");

                    }
                    else {
                        _this.Dialog_CreateCouponObj.close();
                        $.L.msgError("创建优惠券失败！");

                    }
                }
            });
        },

        //提交红包
        Ajax_AddRPInfo: function (text) {
            if (text == '拼手气红包') {
                var data = $("#LuckyRP").serialize();
            }
            else {
                var data = $("#NormalRP").serialize();
            }
            //console.log(data)
            $.L.ajaxHandle({
                url: ACTION_URL("JackpotAdd", "Market"),
                data: data,
                success: function (response) {
                    if (response > 0) {
                        $.MGJ.JackpotManage.JackpotTable(psize, 1);//渲染列表
                        _this.Dialog_CreateRPObj.close();

                        $.L.msgConfirm("创建成功！ 是否立即授权?", function () {
                            Ajax_Accredit(response);
                        });

                    }
                    else {
                        $.L.msgError("创建红包失败！");
                        _this.Dialog_CreateRPObj.close();
                    }
                }
            });
        },
        Ajax_DelJackpot: function (id, type) {

            $.L.ajaxHandle({
                url: ACTION_URL("DelJackpot", "Market"),
                data: { Id: id },
                success: function (response) {
                  
                    if (response.state == 1) {
                        $.L.msgError("该奖品绑定了活动  不能被删除.  请先删除有关的活动。");
                    }
                    else if (response.state == 2) {
                        $.L.msgError("  不能被删除！请先禁用 ");
                    }
                    else   {
                        $.MGJ.JackpotManage.JackpotTable(psize, type);//渲染列表
                        $.L.msgSuccess("操作成功！");
                    }
                    
                }
            });
        },
        //提交流量包
        Ajax_AddLLBInfo: function () {
            var data = $("#LLBPacket").serialize();
            $.L.ajaxHandle({
                url: ACTION_URL("JackpotAdd", "Market"),
                data: data,
                success: function (response) {
                    if (response > 0) {
                        $.MGJ.JackpotManage.JackpotTable(psize, 3);//渲染列表
                        _this.Dialog_CreateFlowObj.close();
                        $.L.msgConfirm("创建成功！ 是否立即授权?", function () {
                            Ajax_Accredit(response);
                        });
                    }
                    else {
                        _this.Dialog_CreateFlowObj.close();
                        $.L.msgError("创建流量包失败！");

                    }
                }
            });
        },
        JackpotTable: function (pnum, type) {
            window.SearchKey = removeSpace($("#Search").val());
            $.ajax({
                type: "post",
                dataType: 'json',
                url: "/Market/JackpotTable",
                data: {
                    pageIndex: pid,
                    pageSize: pnum,
                    type: type,
                    search: window.SearchKey
                },
                async: false, //
                beforeSend: function () {
                    $.L.loadingIcon();

                },
                success: function (data) {
                    if (type === 1) {
                        _this.PromotionRedDate(data);
                    }
                    else if (type === 2) {
                        _this.PromotionYHJDate(data);
                    }
                    else {
                        _this.PromotionLLBDate(data);
                    }
                    setTimeout(function () {
                        $.L.loadingIconClose();
                    }, 500)
                }

            });
        },
        url: function (jid, type, couponType) {
            $.MGJ.Common.loadurl(this, '/Market/JackpotDetails?jid=' + jid + '&type=' + type + '&couponType=' + couponType);
        },
        //加载优惠券列表
        PromotionYHJDate: function (data) {
            $("#YHJDate .result-con").remove();
            pattern = new RegExp("\\((.*)+?\\)");
            $(data.PageData.reverse()).each(function (i, arr) {
                arr.Remark = arr.Remark || "";
                var StartDate = arr.StartDate.match(pattern)[1];
                var EndtDate = arr.EndDate.match(pattern)[1];
                var name = arr.CouponType == 0 ? "代金券" : "折扣劵";
                //var flag = arr.ViewFlag == 0 ?  "未授权 " : "已启用";
                var flag = arr.ViewFlag == 0
                    ? '<span style="color: #EE5F51" class="flag">未授权 </span> <span style="color: #50AE55" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即授权</span> '
                    : '<span style="color: #50AE55" class="flag">已启用 </span> <span style="color: #EE5F51" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即禁用</span> ';
                //0:代金券 1：折扣卷  
                var money = arr.CouponType == 0 ? arr.Denomination + "<span>元优惠券</span>" : parseFloat(arr.Denomination).toFixed(2) + "<span>折</span>";
                var $tr = ' <div class="result-con"  id= ' + arr.Id + '> ' +
                    '<div class="red-con">' +
                    ' <div> ' +
                    '<p>' + name + ' </p> ' +
                    '<p>' + money + ' </p>' +
                    '<p>' + getLocalTime(StartDate) + ' - ' + getLocalTime(EndtDate) + '</p>' +
                    '</div> ' +
                    '<div> ' +
                    '<p>创建人：' + arr.ChangeLoginName + '</p> ' +
                     '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                    '<p style="height:16px;">' + arr.Name + '</p> ' +
                    '<p> ' + flag + ' </p>' +
                    ' </div> ' +
                    '</div> ' +
                    '<span>满' + arr.UseCondition + '元使用</span> ' +
                    '<span> <p>有效期：' + getLocalTime(StartDate) + ' </p> <p>结束时间：' + getLocalTime(EndtDate) + '</p> </span>' +
                    '<span>' + arr.Remark + '</span> ' +
                     '<span><a>查看详情</a><a style="margin-left: 10px;"  onclick="$.MGJ.JackpotManage.Ajax_DelJackpot(' + arr.Id + ',2)">删除</a></span> ' +
                    ' </div>';
                $('#YHJDate .result-title').after($tr);
            });

            if (data.TotalItemCount > psize) {
                var moreLen = $("#YHJDate a.more").length;
                if (moreLen == 0) {
                    $('#YHJDate').append('<a  style="color: red;" class="more">更多数据...</span>');
                }
                $('#YHJDate .more').bind('click', function () {
                    _this.JackpotTable(data.TotalItemCount, 2);
                    $('#YHJDate .more').remove();

                })
            }
        },
        //加载红包列表
        PromotionRedDate: function (data) {

            $("#redPacketData .result-con").remove();
            $(data.PageData.reverse()).each(function (i, arr) {
                arr.Remark = arr.Remark || "";
                var $tr = '';
                var type = arr.CouponType == 0 ? "拼手气红包 " : "普通红包";
                var flag = arr.ViewFlag == 0
                   ? '<span style="color: #EE5F51" class="flag">未授权 </span> <span style="color: #50AE55" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即授权</span> '
                   : '<span style="color: #50AE55" class="flag">已启用 </span> <span style="color: #EE5F51" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即禁用</span> ';

                //console.log(arr)
                if (arr.CouponType == 0) {
                    $tr = ' <div class="result-con"  id= ' + arr.Id + '> ' +
                   '<div class="red-con">' +
                   '<div> ' +
                  '<p>' + arr.Name + '[拼手气]</p> ' +
                  '<p> <span>￥</span>' + arr.TotalMoney + ' </p>' +
                  '<p>￥' + arr.MinDenomination + ' - ￥' + arr.MaxDenomination + '</p>' +
                   '</div>' +
                   '<div> ' +
                   '<p>创建人：' + arr.ChangeLoginName + '</p> ' +
                   '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                   '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                   '<p>' + flag + '  </p>' +
                   ' </div> ' +
                   '</div> ' +
                    '<span><div>发放总数量：' + arr.TotalNums + '</div> <div> 剩余总数量：' + arr.ResidueNums + '</div></span> ' +
                    '<span><div>预算总金额：' + arr.TotalMoney + '</div><div>剩余总金额：' + arr.ResidueMoney + '</div></span>' +
                  '<span>' + arr.Remark + '</span> ' +
                   '<span><a  onclick="$.MGJ.JackpotManage.url(' + arr.Id + ',' + arr.Type + ',' + arr.CouponType + ')">查看详情</a><a  onclick="$.MGJ.JackpotManage.Ajax_DelJackpot(' + arr.Id + ',1)" style="margin-left: 10px;">删除</a></span> ' +
                   ' </div>';
                }
                else {
                    $tr = ' <div class="result-con"  id= ' + arr.Id + '> ' +
                    '<div class="red-con">' +
                    '<div> ' +
                   '<p>' + arr.Name + '[普通]</p> ' +
                   '<p> <span>￥</span>' + arr.TotalMoney + ' </p>' +
                   '<p>￥' + arr.Denomination + '</p>' +
                    '</div>' +
                    '<div> ' +
                    '<p>创建人：' + arr.ChangeLoginName + '</p> ' +
                   '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                    '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                   '<p>' + flag + '  </p>' +
                   ' </div> ' +
                    '</div> ' +
                     '<span><div>发放总数量：' + arr.TotalNums + '</div> <div> 剩余总数量：' + arr.ResidueNums + '</div></span> ' +
                    '<span><div>预算总金额：' + arr.TotalMoney + '</div><div>剩余总金额：' + arr.ResidueMoney + '</div></span>' +
                   '<span>' + arr.Remark + '</span> ' +
                      '<span><a  onclick="$.MGJ.JackpotManage.url(' + arr.Id + ',' + arr.Type + ',' + arr.CouponType + ')">查看详情</a><a  onclick="$.MGJ.JackpotManage.Ajax_DelJackpot(' + arr.Id + ',1)" style="margin-left: 10px;">删除</a></span> ' +
                    ' </div>';
                }
                $('#redPacketData .result-title').after($tr);
            });


            if (data.TotalItemCount > psize) {
                var moreLen = $("#redPacketData a.more").length;
                if (moreLen == 0) {
                    $('#redPacketData').append('<a  style="color: red;" class="more">更多数据...</span>');
                }
                $('#redPacketData .more').bind('click', function () {
                    _this.JackpotTable(data.TotalItemCount, 1);
                    $('#redPacketData .more').remove();

                })
            }
        },
        //加载流量包列表
        PromotionLLBDate: function (data) {
            $("#LLBDate .result-con").remove();
            pattern = new RegExp("\\((.*)+?\\)");
            $(data.PageData.reverse()).each(function (i, arr) {
                arr.Remark = arr.Remark || "";
                var flag = arr.ViewFlag == 0
                   ? '<span style="color: #EE5F51" class="flag">未授权 </span> <span style="color: #50AE55" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即授权</span> '
                   : '<span style="color: #50AE55" class="flag">已启用 </span> <span style="color: #EE5F51" class="empower" onclick="Ajax_Accredit(' + arr.Id + ')">立即禁用</span> ';
                var type = arr.LLBType == 1 ? "电信流量" : arr.LLBType == 2 ? "移动流量" : arr.LLBType == 3 ? "联通流量" : "通用流量";
                var $tr = ' <div class="result-con"  id= ' + arr.Id + '> ' +
                    '<div class="red-con">' +
                    ' <div> ' +
                    '<p>' + arr.Name + ' </p> ' +
                    '<p>' + arr.Denomination + ' 元</p>' +
                    '<p></p>' +
                    '</div> ' +
                    '<div> ' +
                    '<p>创建人：' + arr.ChangeLoginName + '</p> ' +
                    '<p>' + type + '</p> ' +
                   '<p style="color: #EAEDF1;height:16px;width:100px"></p> ' +
                    '<p> ' + flag + ' </p>' +
                    ' </div> ' +
                    '</div> ' +
                    '<span><div>发放数量：' + arr.TotalNums + '</div> <div> 剩余数量：' + arr.ResidueNums + '</div></span> ' +
                    '<span><div>发放流量：' + arr.TotalMoney + '</div><div>剩余流量：' + arr.ResidueMoney + '</div></span>' +
                    '<span>' + arr.Remark + '</span>' +
                    '<span><a  onclick="$.MGJ.JackpotManage.url(' + arr.Id + ',' + arr.Type + ',' + arr.CouponType + ')">查看详情</a><a  onclick="<a  onclick="$.MGJ.JackpotManage.Ajax_DelJackpot(' + arr.Id + ',3)" style="margin-left: 10px;">删除</a></span>' +
                    ' </div>';
                $('#LLBDate  .result-title').after($tr);
            });

            if (data.TotalItemCount > psize) {
                var moreLen = $("#LLBDate a.more").length;
                if (moreLen == 0) {
                    $('#LLBDate').append('<a  style="color: red;" class="more">更多数据...</span>');
                }
                $('#LLBDate .more').bind('click', function () {
                    _this.JackpotTable(data.TotalItemCount, 3);
                    $('#LLBDate .more').remove();
                })
            }
        }

    });
})(jQuery);

//详细数据
(function () {
    var _this = $.MGJ.JackpotManage;
    $.extend($.MGJ.JackpotManage, {
        Bin_ViewFlag: function (text) { return text == 1 ? "已启用" : "已禁用" },
        //奖池领奖详细数据
        Ajax_Thewinning: function (pageIndex, jid) {
            $.L.ajaxHandle({
                url: ACTION_URL("GetTheWinningList", "Market"),
                data: {
                    jId: jid,
                    pageIndex: pageIndex,
                    pageSize: 6
                },
                success: function (response) {
                    $("#Thewinning").L_bindingDataList(response);
                }
            });
        },
        Ajax_GetJackpotModel: function (jid) {
            $.L.ajaxHandle({
                url: ACTION_URL("GetJackpotModel", "Market"),
                data: {
                    jId: jid
                },
                success: function (response) {
                    var arr = [];
                    var nameType = response.Name;
                    arr.push(response);
                    $("#JackpotModel").L_bindingDataList(arr);
                    //优惠券类型0代金券1折扣券枚举    在程序中存在红包类型手气红包0普通红包1
                    if (response.CouponType == 0) {
                        $('#nameType').text(nameType + '[拼手气]');
                    }
                    else if (response.CouponType == 1) {
                        $('#nameType').text(nameType + '[普通]')
                    }
                }
            });
        }
        ,
        JackpotName_type: function () {


        },
        Ajax_GetJackpot_Activity: function (jid) {
            $.L.ajaxHandle({
                url: ACTION_URL("GetJackpot_ActivityTable", "Market"),
                data: {
                    pageIndex: 1,
                    pageSize: 6,
                    jId: jid
                },
                success: function (response) {
                    //console.log(response)
                    $("#GetJackpot_Activity").L_bindingDataList(response);
                }
            });
        },
        //微信名base64解密
        Bin_Base64Nickname: function (txt) {
            return decode(txt);
        }
    });
})(jQuery);




